/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.world;

import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.exceptions.InvalidFileException;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 * @author Massimo Canonaco
 * @author Fabio Rende
 */
public class MapManager {

	private static final String SPLIT = ";";
	private MapFileManager mapBuilder;
	private String mapFolder;
	private Map currentMap = new Map();
	
	private LinkedList<String>maps;
	private ListIterator<String> current;
	
//	private Iterator<String> previous;
	
	/** */
	public MapManager(MapFileManager fileHandler)
	{
		mapBuilder = fileHandler;
//		mapNames = new LinkedList<>();
//		currentMapName = -1;
	}
	
	public MapManager(MapFileManager fileHandler, String mapFolder)
	{
		this.mapFolder = mapFolder;
		mapBuilder = fileHandler;
	}

	/**
	 * @return the builder
	 */
	public MapFileManager getBuilder() {
		return mapBuilder;
	}

	/**
	 * @param builder the builder to set
	 */
	public void setBuilder(MapFileManager builder) {
		this.mapBuilder = builder;
	}

	/**
	 * @return the folder
	 */
	public String getFolder() {
		return mapFolder;
	}

	/**
	 * @param folder the folder to set
	 */
	public void setFolder(String folder) {
		this.mapFolder = folder;
	}
	
	/**
	 * @throws InvalidFileException 
	 * @throws IOException 
	 * @throws FileNotFoundException  */
	public Map loadCurrentMap() throws FileNotFoundException, IOException, InvalidFileException
	{
//		previous = current.;
		return currentMap = mapBuilder.build(mapFolder, current.next());
	}
	
	/** */
	public boolean isLastMap()
	{
		return !current.hasNext();
	}
	
	/** */
	public Map loadMap(final String name) throws FileNotFoundException, IOException, InvalidFileException
	{
		Map map = new Map();
		map = mapBuilder.build(mapFolder, name);
		return map;
	}
	/** */
	public void init(final String mapCicle) throws FileNotFoundException
	{
//		System.out.println("INIT MAPCOSO");
		maps = new LinkedList<>();
		Scanner reader = new Scanner(new FileInputStream(mapCicle));
		
		while(reader.hasNext())
		{
			StringTokenizer string = new StringTokenizer(reader.nextLine(), SPLIT);
			while(string.hasMoreTokens())
				maps.add(string.nextToken());
		}
		reader.close();
		current = maps.listIterator();
//		previous = maps.iterator();
	}
	
	/** */
	public Map nextMap() throws FileNotFoundException, IOException, InvalidFileException
	{
		if(current.hasNext())
		{
			return loadCurrentMap();
		}
		
		return null;
	}
	
	/** */
	public void jumpBack()
	{
		if(current != null && current.hasPrevious())
			current.previous();
//		System.err.println("Chiamato jumpback");
	}

	public Map getCurrentMap() {
		return currentMap;
	}

	public void setCurrentMap(Map currentMap) {
		this.currentMap = currentMap;
	}
}
